home *** CD-ROM | disk | FTP | other *** search
/ Mac Format 1994 October / Macformat17.cdr / Shareware City / Developers / shutdown-fx-201-c / sfx ƒ / sfx control app ƒ / Shell ƒ / buttons.c < prev    next >
Text File  |  1994-07-11  |  6KB  |  199 lines

  1. /**********************************************************************\
  2.  
  3. File:        buttons.c
  4.  
  5. Purpose:    This module handles 3D buttons: drawing, clicking, tracking.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program in a file named "GNU General Public License".
  19. If not, write to the Free Software Foundation, 675 Mass Ave,
  20. Cambridge, MA 02139, USA.
  21.  
  22. \**********************************************************************/
  23.  
  24. #include "buttons.h"
  25.  
  26. static    RGBColor        gBlack4Bit={32768, 32768, 32768};
  27. static    RGBColor        gWhite4Bit={60948, 60948, 60948};
  28. static    RGBColor        gBackground4Bit={49152,49152,49152};
  29. static    RGBColor        gBlack8Bit={26214, 26214, 39322};
  30. static    RGBColor        gWhite8Bit={60948, 60948, 60948};
  31. static    RGBColor        gBackground8Bit={52429, 52429, 52429};
  32.  
  33. void Draw3DButton(Rect *buttonRect, unsigned char *theTitle,
  34.     Handle iconHandle, short buttonDepth, Boolean isDown)
  35. {
  36.     FontInfo        theFontInfo;
  37.     Rect            tempRect;
  38.     RGBColor        oldForeColor, oldBackColor;
  39.     PixPatHandle    backgroundppat;
  40.     short            theLineHeight;
  41.     Rect            iconRect;
  42.     
  43.     TextFont(geneva);
  44.     TextSize(9);
  45.     TextFace(0);
  46.     GetFontInfo(&theFontInfo);
  47.     theLineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
  48.     if (iconHandle!=0L)
  49.     {
  50.         iconRect.left=(buttonRect->right-buttonRect->left)/2+buttonRect->left-16+
  51.             (isDown ? 1 : 0);
  52.         iconRect.right=iconRect.left+32;
  53.         iconRect.top=(buttonRect->bottom-buttonRect->top)/2+buttonRect->top-16+
  54.             (isDown ? 1 : 0)-((theTitle!=0L) ? 5 : 0);
  55.         iconRect.bottom=iconRect.top+32;
  56.     }
  57.     
  58.     if (buttonDepth>2)
  59.     {
  60.         GetForeColor(&oldForeColor);
  61.         GetBackColor(&oldBackColor);
  62.         
  63.         backgroundppat=NewPixPat();
  64.         MakeRGBPat(backgroundppat, (buttonDepth==4) ? &gBackground4Bit :
  65.             &gBackground8Bit);
  66.         SetRect(&tempRect, buttonRect->left+2, buttonRect->top+2,
  67.             buttonRect->right-2, buttonRect->bottom-2);
  68.         
  69.         FillCRect(&tempRect, backgroundppat);
  70.  
  71.         RGBBackColor(white);
  72.         RGBForeColor(isDown ? ((buttonDepth==4) ? &gBlack4Bit : &gBlack8Bit) :
  73.             ((buttonDepth==4) ? &gWhite4Bit : &gWhite8Bit));
  74.         MoveTo(buttonRect->left, buttonRect->top);
  75.         LineTo(buttonRect->right-2, buttonRect->top);
  76.         MoveTo(buttonRect->left, buttonRect->top+1);
  77.         LineTo(buttonRect->right-3, buttonRect->top+1);
  78.         MoveTo(buttonRect->left, buttonRect->top+2);
  79.         LineTo(buttonRect->left, buttonRect->bottom-2);
  80.         MoveTo(buttonRect->left+1, buttonRect->top+2);
  81.         LineTo(buttonRect->left+1, buttonRect->bottom-3);
  82.         
  83.         RGBForeColor(isDown ? ((buttonDepth==4) ? &gWhite4Bit : &gWhite8Bit) :
  84.             ((buttonDepth==4) ? &gBlack4Bit : &gBlack8Bit));
  85.         MoveTo(buttonRect->right-1, buttonRect->top);
  86.         LineTo(buttonRect->right-1, buttonRect->bottom-1);
  87.         MoveTo(buttonRect->right-2, buttonRect->top+1);
  88.         LineTo(buttonRect->right-2, buttonRect->bottom-1);
  89.         MoveTo(buttonRect->left+2, buttonRect->bottom-2);
  90.         LineTo(buttonRect->right-1, buttonRect->bottom-2);
  91.         MoveTo(buttonRect->left+1, buttonRect->bottom-1);
  92.         LineTo(buttonRect->right-1, buttonRect->bottom-1);
  93.     }
  94.     else
  95.     {
  96.         EraseRect(buttonRect);
  97.         FrameRect(buttonRect);
  98.         if (!isDown)
  99.         {
  100.             MoveTo(buttonRect->left+2, buttonRect->bottom-2);
  101.             LineTo(buttonRect->right-2, buttonRect->bottom-2);
  102.             LineTo(buttonRect->right-2, buttonRect->top+2);
  103.         }
  104.         else
  105.         {
  106.             MoveTo(buttonRect->left+1, buttonRect->bottom-3);
  107.             LineTo(buttonRect->left+1, buttonRect->top+1);
  108.             LineTo(buttonRect->right-3, buttonRect->top+1);
  109.         }
  110.     }
  111.     
  112.     if (theTitle!=0L)
  113.     {
  114.         ForeColor(blackColor);
  115.         MoveTo(buttonRect->left+(buttonRect->right-buttonRect->left)/2-
  116.             (StringWidth(theTitle)/2)+(isDown ? 1 : 0), ((iconHandle!=0L) ?
  117.             buttonRect->bottom-6 : buttonRect->top+
  118.             (buttonRect->bottom-buttonRect->top)/2+theLineHeight/2-2)+
  119.             (isDown ? 1 : 0));
  120.         DrawString(theTitle);
  121.     }
  122.     
  123.     if (iconHandle!=0L)
  124.     {
  125.         if (buttonDepth>2)
  126.             PlotCIcon(&iconRect, (CIconHandle)iconHandle);
  127.         else
  128.         {
  129.             BitMap    iconMap;
  130.             GrafPtr curPort;
  131.             
  132.             GetPort(&curPort);
  133.             HLock( iconHandle);    /* lock data in place */
  134.             
  135.             iconMap.baseAddr = *iconHandle;    /* dereference the handle */
  136.             iconMap.rowBytes = 4;            /* setup other fields */
  137.             SetRect( &iconMap.bounds, 0,0,32,32);
  138.             
  139.             CopyBits( &iconMap, &(curPort->portBits),
  140.                          &iconMap.bounds, &iconRect, srcOr, 0L );
  141.             
  142.             HUnlock(iconHandle);                /* all done; let it float */
  143.         }
  144.     }
  145.     
  146.     if (buttonDepth>2)
  147.     {
  148.         DisposPixPat(backgroundppat);
  149.         RGBForeColor(&oldForeColor);
  150.         RGBBackColor(&oldBackColor);    
  151.     }
  152. }
  153.  
  154. Boolean Track3DButton(Rect *buttonRect, unsigned char *theTitle,
  155.     Handle iconHandle, short buttonDepth)
  156. {
  157.     Point            mouseLoc;
  158.     Boolean            buttonIsDown;
  159.     
  160.     buttonIsDown=FALSE;
  161.     while (StillDown())
  162.     {
  163.         GetMouse(&mouseLoc);
  164.         if (PtInRect(mouseLoc, buttonRect))
  165.         {
  166.             if (!buttonIsDown)
  167.             {
  168.                 buttonIsDown=TRUE;
  169.                 Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth,
  170.                     TRUE);
  171.             }
  172.         }
  173.         else
  174.         {
  175.             if (buttonIsDown)
  176.             {
  177.                 buttonIsDown=FALSE;
  178.                 Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth,
  179.                     FALSE);
  180.             }
  181.         }
  182.     }
  183.     
  184.     if (buttonIsDown)
  185.         Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, FALSE);
  186.     
  187.     return buttonIsDown;
  188. }
  189.  
  190. void Hit3DButton(Rect *buttonRect, unsigned char *theTitle,
  191.     Handle iconHandle, short buttonDepth)
  192. {
  193.     unsigned long        dummy;
  194.     
  195.     Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, TRUE);
  196.     Delay(8, &dummy);
  197.     Draw3DButton(buttonRect, theTitle, iconHandle, buttonDepth, FALSE);
  198. }
  199.